package com.array;


import com.tgy.util.AbstractList;

/**
 * @Author: tgy
 * @Date: 2020-08-25 09:55
 */
public class ArrayList<E> extends AbstractList<E> {

    private Object[] arrays;


    private static final int INIT_CAPACITY = 10;

    private static  final int NOT_FOUND_ELEMENT = -1;


    public ArrayList() {

       this(INIT_CAPACITY);
    }

    public ArrayList(int capacity){

        this.arrays =  new Object[capacity > INIT_CAPACITY ? capacity : INIT_CAPACITY];
        size = 0;
    }



    /**
     * 获取指定位置的值
     * @param index
     * @return
     */
    @Override
    public E get(int index){

        checkIndex(index);
        return (E) this.arrays[index];
    }


    /**
     * 在指定位置插入值
     * @param index
     * @param element
     * @return
     */
    @Override
    public E set(int index,E element){


        checkIndex(index);

        E old = (E) this.arrays[index];

        this.arrays[index] = (Object) element;

        return old;
    }


    /**
     * 向index位置添加元素,当前位置的元素往后移动
     * @param index
     * @param element
     */
    @Override
    public void add(int index, E element){

        checkAddIndex(index);

        ensureCapacity(size + 1);

        for (int i = size; i > index; i--) {

            this.arrays[i] = this.arrays[i - 1];
        }

        this.arrays[index] = (Object) element;
        size++;

    }


    /**
     * 删除指定位置的元素
     * @param index
     * @return
     */
    @Override
    public E remove(int index){

        checkIndex(index);

        E element = (E) this.arrays[index];

        for (int i = index ; i < size; i++) {

            this.arrays[i] = this.arrays[i + 1];
        }

        arrays[--size] = null;

        return element;
    }

    /**
     * 获取元素所在的位置
     * @param element
     * @return
     */
    @Override
    public int indexof(E element){


        /**
         * 由于 null不能调用 equals方法，所以要区别对待
         */
        if (element == null){

            for (int i = 0; i < size; i++) {

                if (this.arrays[i] == null){

                    return  i;
                }
            }

        }else {

            for (int i = 0; i < size; i++) {

                if (element.equals(this.arrays[i])) {

                    return i;
                }
            }
        }



        return NOT_FOUND_ELEMENT;
    }

    /**
     * 清除数组
     */
    @Override
    public void clear(){

        for (int i = 0; i < size ; i++) {

            arrays[i] = null;
        }

        this.size = 0;
    }



    @Override
    public String toString() {


        StringBuilder stringBuilder = new StringBuilder("size=");

        stringBuilder.append(size).append(" array[");

        for (int i = 0; i < size; i++) {

            if (i != 0){

                stringBuilder.append(",");
            }

            stringBuilder.append(arrays[i]);

        }

        stringBuilder.append("]");

        return stringBuilder.toString();
    }

    private void ensureCapacity(int i) {


        int capacity = arrays.length;

        if (capacity >= i){
            return;
        }

        capacity = capacity + (capacity>>1);

        Object[] newArrays = new Object[capacity];

        for (int j = 0; j < size ; j++) {

            newArrays[j] =  arrays[j];
        }

        arrays = newArrays;

        System.out.println("扩容到 " + capacity);
    }

}
